package com.module.wx.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.module.wx.domain.ZzdCouponMain;
import com.module.wx.service.IZzdCouponMainService;
import com.module.wx.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.module.wx.domain.ZzdCouponNeckState;
import com.module.wx.service.IZzdCouponNeckStateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author hezhengxian
 * @date 2023-03-06
 */
@RestController
@RequestMapping("/couponNeck")
public class ZzdCouponNeckStateController extends BaseController
{
    @Autowired
    private IZzdCouponNeckStateService zzdCouponNeckStateService;
    @Autowired
    private IZzdCouponMainService IZzdCouponMainService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("wx:couponNeck:list")
    @GetMapping("/list")
    public TableDataInfo list(ZzdCouponNeckState zzdCouponNeckState)
    {
        startPage();
        List<ZzdCouponNeckState> list = zzdCouponNeckStateService.selectZzdCouponNeckStateList(zzdCouponNeckState);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("wx:couponNeck:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZzdCouponNeckState zzdCouponNeckState)
    {
        List<ZzdCouponNeckState> list = zzdCouponNeckStateService.selectZzdCouponNeckStateList(zzdCouponNeckState);
        ExcelUtil<ZzdCouponNeckState> util = new ExcelUtil<ZzdCouponNeckState>(ZzdCouponNeckState.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("wx:couponNeck:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(zzdCouponNeckStateService.selectZzdCouponNeckStateById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("wx:couponNeck:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ZzdCouponNeckState zzdCouponNeckState)
    {
        zzdCouponNeckState.setId(Util.get_radom_id());
        zzdCouponNeckState.setNeckDate(new Date());
        return toAjax(zzdCouponNeckStateService.insertZzdCouponNeckState(zzdCouponNeckState));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("wx:couponNeck:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZzdCouponNeckState zzdCouponNeckState)
    {
        return toAjax(zzdCouponNeckStateService.updateZzdCouponNeckState(zzdCouponNeckState));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("wx:couponNeck:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(zzdCouponNeckStateService.deleteZzdCouponNeckStateByIds(ids));
    }

    /**
     *  根据用户id查询用户可用优惠券
     * @param userid
     * @return
     */
    @RequiresPermissions("wx:couponNeck:query")
    @GetMapping(value = "/only/{userid}")
    public AjaxResult getCouponByUserId(@PathVariable("userid") String userid)
    {
        // 根据用户领用优惠券信息，查询优惠券详情
        List<ZzdCouponNeckState> zzdCouponNeckStateList = zzdCouponNeckStateService.selectZzdCouponNeckStateByUserId(userid);
        // 如果用户领用了优惠券
        if(zzdCouponNeckStateList.size()>0){
            for (int i = 0; i < zzdCouponNeckStateList.size(); i++) {
                List<ZzdCouponMain> list = new ArrayList<ZzdCouponMain>();
                list.add(IZzdCouponMainService.selectZzdCouponMainById(zzdCouponNeckStateList.get(i).getCouponId()));
                zzdCouponNeckStateList.get(i).setZzdCouponMainList(list);
            }
        }
        return success(zzdCouponNeckStateList);
    }

    /**
     *  根据用户id查询用户所有优惠券
     * @param userid
     * @return
     */
    @RequiresPermissions("wx:couponNeck:query")
    @GetMapping(value = "/all/{userid}")
    public AjaxResult getAllCouponByUserId(@PathVariable("userid") String userid)
    {
        // 根据用户领用优惠券信息，查询优惠券详情
       List<ZzdCouponNeckState> zzdCouponNeckStateList = zzdCouponNeckStateService.selectAllZzdCouponNeckStateByUserId(userid);
       // 如果用户领用了优惠券
       if(zzdCouponNeckStateList.size()>0){
           for (int i = 0; i < zzdCouponNeckStateList.size(); i++) {
               List<ZzdCouponMain> list = new ArrayList<ZzdCouponMain>();
               list.add(IZzdCouponMainService.selectZzdCouponMainById(zzdCouponNeckStateList.get(i).getCouponId()));
               zzdCouponNeckStateList.get(i).setZzdCouponMainList(list);
           }
       }
        return success(zzdCouponNeckStateList);
    }
}
